package com.tau.egonetwork.gui.core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.tau.egonetwork.common.Log;
import com.tau.egonetwork.gui.interfaces.IPopupAnswerToTab;
import com.tau.egonetwork.gui.interfaces.IRequestGUI;

public abstract class NetworkTab extends BasicTab {

    protected IRequestGUI _networkRequestGUI;

    protected abstract Image getBackground();
    protected abstract Image getLogo();
    protected abstract Image getNewButtonImage();
    protected abstract Image getOpenButtonImage();
    
    protected abstract void onOpenButtonClicked(Shell parent);
    protected abstract void openPopup(Display display, Shell shell, IPopupAnswerToTab popupAnswer);
    
    public NetworkTab(IRequestGUI guiRequest) { 
    	_networkRequestGUI = guiRequest;
    }
    
    public void issueNewProfile(final Shell shell) {
    	openPopup(shell.getDisplay(), shell, new IPopupAnswerToTab() {
			
			@Override
			public void popupClosedWithCode(int code) {
				Log.writeLineToLog("popupClosedWithCode " + code);
				if (code == SWT.RETRY)
					issueNewProfile(shell);
			}
		});
    }
    
	@Override
	protected boolean onTabClosed() {
		return true;
	}
    
	@Override
	protected void buildTab(final Composite tabComposite) {
        
        tabComposite.setBackgroundImage(getBackground());
        tabComposite.setBackgroundMode(SWT.INHERIT_FORCE);
        
        Composite facebookComposite = new Composite(tabComposite, SWT.NONE);
        
        GridLayout facebookCompositeLayout = new GridLayout(5, true);
        facebookCompositeLayout.verticalSpacing = 0;
        facebookCompositeLayout.horizontalSpacing = 0;
        facebookComposite.setLayout(facebookCompositeLayout);
        facebookComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        
        Label imgLabel = new Label(facebookComposite, SWT.NONE);
        imgLabel.setImage(getLogo());
        GridData imgGridData = new GridData(SWT.CENTER, SWT.CENTER, true, true);
        imgGridData.horizontalSpan = 2;
        imgLabel.setLayoutData(imgGridData);
        
        Composite buttonsComposite = new Composite(facebookComposite, SWT.NONE);
        GridLayout buttonsGridLayout = new GridLayout(1, true);
        buttonsGridLayout.verticalSpacing = 40;
        buttonsComposite.setLayout(buttonsGridLayout);
        GridData buttonsGridData = new GridData(SWT.CENTER, SWT.CENTER, true, true);
        buttonsGridData.horizontalSpan = 3;
        buttonsComposite.setLayoutData(buttonsGridData);
        
        final Button newProfileButton = new Button(buttonsComposite, SWT.PUSH);
        newProfileButton.setText("    New " + getTabText().trim() + " Profile");
        Image newFBImg = getNewButtonImage();
        newProfileButton.setImage(newFBImg);
        GridData newFBGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        newFBGridData.heightHint = newFBImg.getImageData().height + 10;
        newProfileButton.setLayoutData(newFBGridData);
        
        final Button openProfileButton = new Button(buttonsComposite, SWT.PUSH);
        openProfileButton.setText("    Open Existing " + getTabText().trim() + " Profile");
        Image openFBImg = getOpenButtonImage();
        openProfileButton.setImage(openFBImg);
        GridData openFBGridData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
        openFBGridData.heightHint = openFBImg.getImageData().height + 10;
        openProfileButton.setLayoutData(openFBGridData);
        
        /* Set both buttons to same size: */
        openProfileButton.addListener(SWT.Resize, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				newProfileButton.setSize(openProfileButton.getSize());
			}
		});
        
        openProfileButton.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				Log.writeLineToLog("NetworkTab (" + getTabText() + ") openProfileButton clicked");
				onOpenButtonClicked(tabComposite.getShell());
			}
		});

        final Display display = tabComposite.getDisplay();
        final IPopupAnswerToTab popupAnswer = new IPopupAnswerToTab() {
			
			@Override
			public void popupClosedWithCode(int code) {
				Log.writeLineToLog("popupClosedWithCode " + code);
				if (code == SWT.RETRY) {
					//issue new push event (so to display popup again from scratch):
					newProfileButton.notifyListeners(SWT.Selection, new Event());
				}
			}
		};
        
        newProfileButton.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				Log.writeLineToLog("NetworkTab (" + getTabText() + ") newProfileButton clicked");
				openPopup(display, tabComposite.getShell(), popupAnswer);
			}
		});
        
	}
}
